﻿using ExcellentProjectPlus.Aplicacion.Catalogos.Catalogos;
using ExcellentProjectPlus.Dominio.Catalogos.Entidades;
using System.Collections.Generic;
using System.Net;
using System.Web.Http;
using System.Web.Http.Description;

namespace Servicios.Catalogos.Controllers
{
    public class CatalogosController : ApiController
    {
        #region Propiedades
        IServicioDeGestionDeCatalogos _servicioDeGestionDeCatalogos;
        #endregion

        #region Constructores
        public CatalogosController(IServicioDeGestionDeCatalogos servicioDeGestionDeCatalogos)
        {
            _servicioDeGestionDeCatalogos = servicioDeGestionDeCatalogos;
        }
        #endregion

        //GET
        [HttpGet]
        [ResponseType(typeof(Catalogo))]
        public IHttpActionResult Get()
        {
            IEnumerable<Catalogo> catalogos = _servicioDeGestionDeCatalogos.ObtenerCatalogos();
            if (catalogos == null)
            {
                return NotFound();
            }

            return Ok(catalogos);
        }


        //GET
        [HttpGet]
        [Route("api/catalogos/{id}")]
        [ResponseType(typeof(Catalogo))]
        public IHttpActionResult GetCatalogo(int id)
        {
            Catalogo catalogo = _servicioDeGestionDeCatalogos.ObtenerCatalogoPorId(id);
            if (catalogo == null)
            {
                return NotFound();
            }

            return Ok(catalogo);
        }

        [HttpPost]
        [ResponseType(typeof(Catalogo))]
        //[Authorize(Roles = "Administrador")]
        public IHttpActionResult Post(Catalogo catalogo)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            _servicioDeGestionDeCatalogos.AgregarCatalogo(catalogo);

            return CreatedAtRoute("DefaultApi", new { id = catalogo.IdCatalogo }, catalogo);
        }

        [HttpPut]
        [Route("api/catalogos/{id}")]
        [ResponseType(typeof(Catalogo))]
        //[Authorize(Roles = "Administrador")]
        public IHttpActionResult Put(int id, [FromBody]Catalogo catalogo)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            if (id != catalogo.IdCatalogo)
                return BadRequest();

            _servicioDeGestionDeCatalogos.ModificarCatalogo(catalogo);

            return StatusCode(HttpStatusCode.NoContent);

        }

        [HttpDelete]
        [Route("api/catalogos/{id}")]
        [ResponseType(typeof(Catalogo))]
        //[Authorize(Roles = "Administrador")]
        public IHttpActionResult Delete(int id)
        {
            Catalogo catalogo = _servicioDeGestionDeCatalogos.ObtenerCatalogoPorId(id);

            if (catalogo == null)
                return NotFound();

            _servicioDeGestionDeCatalogos.EliminarCatalogo(catalogo);
            return Ok(catalogo);
        }

        private bool CatalogoExists(int id)
        {
            return _servicioDeGestionDeCatalogos.ObtenerCatalogoPorId(id) != null;
        }


        [HttpGet()]
        // [AllowAnonymous]
        [Route("api/catalogos/GetPagedCatalogos/{pageIndex}/{pageSize}")]
        public IEnumerable<Catalogo> GetPagedCatalogos(int pageIndex, int pageSize)
        {
            return _servicioDeGestionDeCatalogos.ObtenerCatalogosPaginado(pageIndex, pageSize);
        }
    }
}